package com.hand.wfive.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;


import com.hand.wfive.R;
import com.hand.wfive.adapter.VipCouponAdapter;
import com.hand.wfive.loginAndVip.model.CouponModel;
import com.hand.wfive.loginAndVip.model.VipActivityModel;
import com.hand.wfive.view.MaxLimitRecyclerView;
import com.hand.wfive.view.countdowntime.CountDownTimerView;

import java.util.List;

public class BuyExitCouponTipsDialog extends Dialog {

    /**
     * 确认和取消按钮
     */
    private TextView btnClose, tvTimeTitle;
    private View viewTime;
    private ImageView btnBuy;
    private CountDownTimerView downTimeView;
    public VipTipListener mListener;
    public MaxLimitRecyclerView rvCoupon;

    private VipCouponAdapter mAdapter;
    private List<CouponModel> listCoupon;
    private VipActivityModel activityConfigModel;
    private long countDownTime;

    public BuyExitCouponTipsDialog(Context context, VipActivityModel vipActivityModel, long countDownTime, VipTipListener listener) {
        super(context, R.style.CustomDialog);
        this.mListener = listener;
        this.countDownTime = countDownTime;
        this.activityConfigModel = vipActivityModel;
        if (vipActivityModel != null) {
            this.listCoupon = vipActivityModel.getCouponList();
        }
    }

    public static void showDialog(Context context, VipActivityModel activityConfigModel, long countDownTime, VipTipListener listener) {
        new BuyExitCouponTipsDialog(context, activityConfigModel, countDownTime, listener).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_buy_exit_coupon_tips);
        setWidth();
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        //初始化界面控件
        initView();
        initAdapter();
        //初始化界面控件的事件
        initEvent();

        startCountDownTime();
    }


    /**
     * 初始化界面控件
     */
    private void initView() {
        btnBuy = findViewById(R.id.buy);
        btnClose = findViewById(R.id.close);
        tvTimeTitle = findViewById(R.id.tvTimeTitle);
        downTimeView = findViewById(R.id.downTimeView);
        viewTime = findViewById(R.id.viewTime);
        rvCoupon = findViewById(R.id.rvCoupon);

        if (listCoupon == null || listCoupon.isEmpty()) {
            tvTimeTitle.setVisibility(View.GONE);
//            btnBuy.setVisibility(View.GONE);
//            downTimeView.setVisibility(View.GONE);
            viewTime.setVisibility(View.GONE);
        }
    }

    private void initAdapter() {
        mAdapter = new VipCouponAdapter();
        rvCoupon.setLayoutManager(new LinearLayoutManager(getContext()));
        rvCoupon.setAdapter(mAdapter);
        mAdapter.setList(listCoupon);
    }

    private void startCountDownTime() {
        downTimeView.setDownTime(countDownTime);
        downTimeView.setDownTimerListener(() -> doFinishTime());
        downTimeView.startDownTimer();
    }

    private void doFinishTime() {
        viewTime.setVisibility(View.GONE);
        tvTimeTitle.setVisibility(View.GONE);
        rvCoupon.setVisibility(View.GONE);
    }

    private void setWidth() {
        //设置宽度
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int) (size.x * 0.8);//设置dialog的宽度为当前手机屏幕的宽度*0.8
        getWindow().setAttributes(p);
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置取消按钮被点击后，向外界提供监听
        btnBuy.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.doBuy();
            }
            dismiss();
        });
        btnClose.setOnClickListener(v -> {
            if (downTimeView != null) {
                downTimeView.cancelDownTimer();
            }
            if (mListener != null) {
                mListener.doClose();
            }
            dismiss();
        });
    }


    public interface VipTipListener {
        void doClose();

        void doBuy();
    }
}
